<?php
namespace Admin\Model;

use Think\Model\RelationModel;

class WxCarModel extends RelationModel
{
    protected $connection = array(
        'db_type'       =>  'mysql',
        'db_host'       =>  '114.55.30.131',
        'db_port'       =>  '3306',
        'db_name'       =>  'wxsmart',
        'db_user'       =>  'zhkj_crm',
        'db_pwd'        =>  'AdkdCGP7fHcjht5c',
        'db_charset'    =>  'utf8',
        'db_debug'      =>  true,
    );
    protected $tablePrefix = '';
    protected $tableName = 'tp_qtcar';

    protected $_auto = array(
    );

    protected $_link = array(
    );

    public function carAudit($id,$status){

        $car = $this->find($id);

        if(!$car)
        {
            return 0;
        }
        $this->where(array("id" => $id))->setField("status",$status);

        if($status == 1)
        {
            //如果没有审核点了审核，则发送模板消息通知已审核
            $first = "您的车辆已通过审核!";
            $keyword1 = "通过";

        }
        else if($status == -1)
        {
            $first = "您的车辆审核失败!";
            $keyword1 = "不通过";
        }

        $verify_time = date("Y-m-d H:i:s",time());
        $dataKey    = 'OPENQT201512171';
        $dataArr    = array(
            'wecha_id'		=> $car['wecha_id'],
            'href'			=> "http://smart.wx91go.com/index.php?g=Wap&m=Qt&a=index&token=".$this->token."&wecha_id=".$car['wecha_id'],
            'first'         => $first,
            'keyword1'      => $keyword1,
            'keyword2'      => $verify_time,
            'remark'        => '感谢您的使用',
        );
        $url = 'http://smart.wx91go.com/index.php?g=Home&m=Index&a=carSendMsg&token=ofanap1448334221';
        $data['dataKey'] = $dataKey;
        $data['dataArr'] = json_encode($dataArr);
        apiPost($url,$data);

        return true;
    }
    public function search($pageSize = 10){
        $status = I('status');
        $start_time = I('start_time');
        $end_time = I('end_time');
        $key = I('key');

        $where = whereTime('addtime');
        if(is_numeric($status))
            $where['status'] = intval($status);
        if($key)
            $where['car_no'] = array('like',"%$key%");

        $count = $this->where($where)->count();
        $page = new \ZL\Page($count, $pageSize, C('PAGE_01'));
        $data['page'] = $page->show();

        /************************************** 取数据 ******************************************/
        $orderData = $this->relation(true)->where($where)->order('addtime desc')->limit($page->limit.','.$page->listRows)->select();
        foreach ($orderData as $k=>&$v) {
            $v['user'] = D('WxuserInfo')->where(['wecha_id'=>$v['wecha_id']])->find();
        }
        $data['data'] = $orderData;
        return $data;
    }
    public function _before_write(&$data)
    {
        parent::_before_write($data); // TODO: Change the autogenerated stub
    }

    public function _after_insert($data, $options)
    {
        parent::_after_insert($data, $options); // TODO: Change the autogenerated stub
    }
}
